function [VNew,hNew,lNew] = watBalRes(VOld,q,a,qTB,param,n)
%
%function [VNew,lNew] = watBalRes(VOld,qOld,a,param,n)
% VOld, qOld, d are all vector entries at time t.
% n is the agent ID.
%
% Tobias Siegfried, 04/15/2007
% revision: 09/30/2008

VNew = VOld + (q - a + param.tBFlow * qTB) * param.dt;
lNew = 0;

if VNew < param.minV % avoid numerical quirks

    %VNew = 0;
    VNew = param.minV;

elseif VNew > param.Vmax(n)

    lNew = (VNew - param.Vmax(n)) / param.dt; % l is the loss
    VNew = param.Vmax(n);

end

hNew = resVH(VNew,param.aCoeff(n),param.bCoeff(n),'vh');